package com.diankangwo.dao.impl;

import com.diankangwo.util.PageSupport;
import org.springframework.stereotype.Repository;
import com.diankangwo.dao.OrderDAO;
import com.diankangwo.domain.Order;

import java.util.ArrayList;
import java.util.List;

@Repository
public class OrderDAOImpl extends BaseDaoImpl<Order> implements OrderDAO {

    @Override
    public PageSupport<Order> findByStatu(String statu,String uid, int pageNo, int pageSize) {
        PageSupport<Order> orderList = new PageSupport<>();
        String hql = "from Order order where 1 = 1 and order.type = 'commodity'";
        if (statu != null && statu != "" && !statu.isEmpty() && !statu.equals("")) {
            hql += " and order.orderStatus = ? and order.uid = ?";
            orderList = findPagination(hql, pageNo,pageSize, statu,uid);

        } else {
            hql += " and order.uid = ?";
            orderList = findPagination(hql,pageNo,pageSize,uid);
        }
        if (orderList == null || orderList.getResultData().size() == 0) {
            return null;
        } else {
            return orderList;
        }
    }

    @Override
    public PageSupport<Order> findByUid(String uid,int pageNo,int pageSize) {
        PageSupport<Order> orderList = new PageSupport<>();
        String hql = "from Order order where 1 = 1 and order.type = 'c' ";
        if (uid != null && uid != "" && !uid.isEmpty() && !uid.equals("")) {
            hql += " and order.uid = ?";
        }
        orderList = findPagination(hql,pageNo,pageSize ,uid);
        if (orderList == null || orderList.getResultData().size() == 0) {
            return null;
        } else {
            return orderList;
        }
    }

    @Override
    public Order findByOrderNo(String orderNo) {
        List<Order> orderList = new ArrayList<Order>();
        orderList = findByQuery("from Order order where orderNo = ?", orderNo);
        if (orderList.size() > 0) {
            return orderList.get(0);
        } else {
            return null;
        }
    }
}
